<?php
    class NaturalExtension_SMMBuilder {
        private $utils;
        private $created_links = array();
        private $default_smm_links = array(
            array(
                'name'  => 'Facebook',
                'ico'   => 'facebook-ico.png',
                'url'   => 'http://www.facebook.com/'
            ),
            array(
                'name'  => 'Twitter',
                'ico'   => 'twitter-ico.png',
                'url'   => 'http://www.twitter.com/'
            ),
            array(
                'name'  => 'AmazonCentral',
                'ico'   => 'amazon-central-ico.png',
                'url'   => 'https://authorcentral.amazon.com/'
            ),
            array(
                'name'  => 'Goodreads',
                'ico'   => 'goodreads-ico.png',
                'url'   => 'http://www.goodreads.com/'
            ),
            array(
                'name'  => 'Shelfari',
                'ico'   => 'shelfari-ico.png',
                'url'   => 'http://www.shelfari.com/'
            ),
            array(
                'name'  => 'LibraryThing',
                'ico'   => 'library-thing-ico.png',
                'url'   => 'http://www.librarything.com/'
            ),
            array(
                'name'  => 'AuthorsXpress',
                'ico'   => 'authors-xpress-ico.png',
                'url'   => 'http://www.authorsxpress.com/'
            )
        );

        public function __construct() {
            $this->utils = new NaturalExtension_Utils();

            // retrieve all links under 'Social Media' category
            $_created_links = get_bookmarks(array(
                'category_name'  => 'Social Media'
            ));

            foreach ($_created_links as $link) {
                $this->created_links[$link->link_name] = $link->link_id;
            }

            $this->load_scripts_and_styles();
        }

        public function load_scripts_and_styles() {
            $this->utils->nq_scripts(
                array('natext-smm-builder', 'natext_smm_builder', array('natext'), $this->utils->config['version'])
            );
        }

        public function generate_service_table() {
            $tab_index = 50;
            $table_headers = array('inc', 'Link Name', 'Link URL', 'Side Note');
            $table_head = '<thead><tr><th>'
                . implode('</th><th>', $table_headers)
                . '</th></tr></thead>';
            $table_body = array('<tbody class="smm-builder-tbody">');

            foreach ($this->default_smm_links as $link) {
                $link_exists    = !! in_array($link['name'], array_keys($this->created_links));
                $elem_disabler  = $link_exists ? 'disabled' : '';

                if ($link_exists) {
                    $curr_link = get_bookmark($this->created_links[$link['name']]);
                }

                $link_name  = $link_exists ? $curr_link->link_name : $link['name'];
                $link_url   = $link_exists ? $curr_link->link_url : $link['url'];
                $link_icon  = OF_DIRECTORY . '/patch/images/icons/smm/' . $link['ico'];
                $cell_elems = array(
                    sprintf('<input %s type="checkbox" tabindex="0" name="include_item">', $elem_disabler),
                    sprintf('<input %s type="text" tabindex="%s" value="%s" placeholder="SMM name..." name="link_name">', $elem_disabler, ++$tab_index, $link_name),
                    sprintf('<input %s type="text" tabindex="%s" value="%s" placeholder="SMM URL..." name="link_url">', $elem_disabler, ++$tab_index, $link_url),
                    ($link_exists ? 'This link already exist.' : '')
                );
                $table_row      = '<tr data-icon="' . $link_icon . '"><td>' . implode('</td><td>', $cell_elems) . '</td></tr>';
                $table_body[]   = $table_row;
            }

            $table_body[] = '</tbody>';

            $table_builder = array(
                '<table class="widefat natext-table smm-builder-table">',
                $table_head,
                implode('', $table_body),
                '</table>'
            );

            echo implode('', $table_builder);
        }
    }
?>